<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\AboutUsModel;
use app\model\BooksModel;
use app\model\RedisModel;
use think\App;

class Books extends Base {

    protected $middleware = [Auth::class];
    private $model;

    public $cacheKey="book:id:";

    private $req;

    private $aid;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->model = new BooksModel();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->aid = $this->isLogin();
    }



    public function getList(){

        $where =[];
        $where[] = ["is_delete","=",0];

       if(!empty($this->req["book_name"])){
            $where[] = ["book_name","LIKE","%".$this->req["book_name"]."%"];
        }

        if(!empty($this->req["book_author"])){
            $where[] = ["book_author","LIKE","%".$this->req["book_author"]."%"];
        }

        if(!empty($this->req["book_status"])){
            $where[] = ["book_status","=",$this->req["book_status"]];
        }

        $list = $this->model->getList($where,$this->page,$this->limit,["created_at"=>"DESC"]);

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }

    /*
     * 获取店铺列表
     * */
    public function getInfo(){

        if(!isset($this->req['book_id']) || empty($this->req['book_id'])){
            return $this->fail("请选择要处理的书籍");
        }


        $info = $this->model->getInfo($this->req['book_id']);

        return $this->succ($info);

    }


    /*
    * 新增/删除/修改
    * */
    public function add()
    {

        if(!isset($this->req['book_name']) || empty($this->req['book_name'])){
            return $this->fail("书籍名称不能为空");
        }

        if(!isset($this->req['book_author']) || empty($this->req['book_author'])){
            return $this->fail("作者名称不能为空");
        }

        if(!isset($this->req['desc']) || empty($this->req['desc'])){
            return $this->fail("书籍描述不能为空");
        }

        if(!isset($this->req['cover_image']) || empty($this->req['cover_image'])){
            return $this->fail("封面图不能为空");
        }

        if($this->model->getTotal(["book_name"=>($this->req['book_name'])]) > 0){
            return $this->fail("书籍已存在");
        }

        $res = $this->model->addOne([[
            "book_name"=>($this->req['book_name']),
            "book_author"=>($this->req['book_author']) ,
            "introduce"=>($this->req['introduce']),
            "desc"=>($this->req['desc']),
            "points"=>($this->req['points']),
            "article_num"=>($this->req['article_num']),
            "cover_image"=>$this->req['cover_image'],
            "is_listen"=>$this->req['is_listen'],
            "created_by"=>$this->aid
        ]]);

        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }



    /*
   * 新增/删除/修改
   * */
    public function edit()
    {

        if(!isset($this->req['book_id']) || empty($this->req['book_id'])){
            return $this->fail("请选择要处理的书籍");
        }

        if(!isset($this->req['book_name']) || empty($this->req['book_name'])){
            return $this->fail("书籍名称不能为空");
        }

        if(!isset($this->req['book_author']) || empty($this->req['book_author'])){
            return $this->fail("作者名称不能为空");
        }

        if(!isset($this->req['desc']) || empty($this->req['desc'])){
            return $this->fail("书籍描述不能为空");
        }

        if(!isset($this->req['cover_image']) || empty($this->req['cover_image'])){
            return $this->fail("封面图不能为空");
        }

        $where =[];
        $where[] = ["book_id","<>",$this->req['book_id']];
        $where[] = ["book_name","=",($this->req['book_name'])];
        if($this->model->getTotal($where) > 0){
            return $this->fail("书籍已存在,不可创建");
        }

        $res = $this->model->updateAll(["book_id"=>$this->req['book_id']],[
            "book_name"=>($this->req['book_name']),
            "book_author"=>($this->req['book_author']) ,
            "introduce"=>($this->req['introduce']),
            "desc"=>($this->req['desc']),
            "article_num"=>($this->req['article_num']),
            "points"=>($this->req['points']),
            "cover_image"=>$this->req['cover_image'],
            "created_by"=>$this->aid,
            "is_listen"=>isset($this->req['is_listen']) ? $this->req['is_listen'] : 0,
            "updated_at"=>date("Y-m-d H:i:s")
        ]);

        if($res){
            $this->delCache($this->req['book_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }

    /*
* 新增/删除/修改
* */
    public function updateStatus()
    {

        if(!isset($this->req['book_id']) || empty($this->req['book_id'])){
            return $this->fail("请选择要处理的书籍");
        }

        if(!isset($this->req['book_status']) || !in_array($this->req['book_status'],[1,-1,2])){
            return $this->fail("请选择操作内容");
        }
        $res = $this->model->updateAll(["book_id"=>$this->req['book_id']],[
            "book_status"=>$this->req['book_status'],
        ]);

        if($res){
            $this->delCache($this->req['book_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    /*
     * 删除书籍
     * */
    public function del()
    {

        if(!isset($this->req['book_id']) || empty($this->req['book_id'])){
            return $this->fail("请选择要处理的书籍");
        }

        $res = $this->model->updateAll(["book_id"=>$this->req['book_id']],[
            "is_delete"=>1,
        ]);

        if($res){
            $this->delCache($this->req['book_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }




    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }
}
